// Unit test for drawCard()

#include "dominion.h"  
#include "dominion_helpers.h"  
#include "rngs.h"  
#include <stdio.h>  
#include <math.h>  
#include <stdlib.h>  
#include <assert.h>

#define MAX_CARD 26

int testDrawCard(int numPlayers, struct gameState *g){
    int player, initialHandCount, finalHandCount;
    player = floor(Random() * (numPlayers) );
    initialHandCount = g->handCount[player];
    g->deckCount[player] = 1;
    drawCard(player, g); // should make deck count == 0 and player count += 1
    finalHandCount = g->handCount[player];
    if(finalHandCount != (initialHandCount + 1))
      return 0;
    // make sure deck count was decremented
    if(g->deckCount[player] != 0) 
      return 0;
    drawCard(player, g); 
    // discard should have been shuffled and put back in deck
    if(g->deckCount < 0)
      return 0; 
    return 1;
}

int main(int argc, char *argv[]) {
  struct gameState g;
  int players, i, j, seed, good, bad, results;
  int k[10] = {adventurer, gardens, great_hall, village, minion, mine, cutpurse, 
           baron, salvager, smithy};
  good = 0;
  bad = 0;
  printf("\nUnit Test 1 -> drawCard() : \n");
  for (j = 0; j < 1000; j++) {
    for (i = 0; i < sizeof(struct gameState); i++) {
      ((char*)&g)[i] = floor(Random() * 256);
    }        
    seed = (long)floor(Random()*(long)argv[1]+j);
    players = j+2;
    // comment out next 2 lines to see bug
    if(players > 4)
      players = players%3+2; // makes sure num players = 2 to 4
    initializeGame(2, k, seed, &g);
    results = testDrawCard(2,&g);
  if(results == 1){
    //  printf("Test Passed!\n");
      good++;
    }
    else{
    //  printf("Test Failed!\n");
      bad++;
    }
  //  printf("\n");
  }
  printf("Tests passed = %i\n", good);
  printf("Tests Failed = %i\n", bad);
  return 0;
}